//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Payton Byrd">
//     Copyright (c) 2007 Payton Byrd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace CBM_Commander.DiskImageSharp
{
    /// <summary>
    /// Generic Image
    /// </summary>
    public class DiskImage
    {
        #region Disk Image Filename
        protected string filename = null;
        protected string path = null;

        public string FullPath
        {
            get { return System.IO.Path.Combine(path, filename); }
            set
            {
                filename = System.IO.Path.GetFileName(value);
                path = System.IO.Path.GetDirectoryName(value);
            }
        }

        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }

        public string Path
        {
            get { return path; }
            set { path = value; }
        }
        #endregion Disk Image Filename

        ImageType type = null;
        List<Block> blocks = null;

        public DiskImage(ImageType type)
        {
            this.type = type;

            blocks = new List<Block>(type.SectorCount);

            for (ushort i = 1; i <= type.Tracks; i++)
            {
                for (ushort j = 0; j < type.TrackSizes[i]; j++)
                {
                    blocks.Add(new Block(i, j));
                }
            }
        }

        public DiskImage(ImageType type, string fullPath)
            : this(type)
        {
            FullPath = fullPath;
        }

        public int GetBlockNumber(Block block)
        {
            return GetBlockNumber(block.Track, block.Sector);
        }

        public int GetBlockNumber(ushort track, ushort sector)
        {
            int result = 0;

            for (int i = 1; i <= track; i++)
            {
                result += type.TrackSizes[i];
            }

            result += sector;

            return result;
        }

        public Block GetNextTrackSectorInChain(Block current)
        {
            ushort track = current.Data[0];
            ushort sector = current.Data[1];

            Block result = null;

            if (track > type.Tracks ||
                sector >= type.TrackSizes[track])
            {
                result = new Block(0, 0);
            }
            else
            {
                result = GetBlock(track, sector);
            }

            return result;
        }

        public Block GetBlock(ushort track, ushort sector)
        {
            Block result = null;

            result = blocks[GetBlockNumber(track, sector)];

            return result;
        }
    }

    public class Test
    {

    }
}
